//
//  fetchJSONFromURL.m
//  JSONHelper
//
//  Created by David Blishen on 23/07/2011.
//

#import "fetchJSONFromURL.h"


@implementation fetchJSONFromURL


- (id)performDefaultImplementation {
	
	
	
	NSURL *url;
	id jsonObject;
	id asRecord;
	NSString *jsonString;
	AEMCodecs *codecs = [[AEMCodecs alloc] init];
	
	// Create the url from the string passed by AppleScript
	
	url=[NSURL URLWithString:[self directParameter]];
	jsonString=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
		
	// Try and parse the JSON
	
	jsonObject = [jsonString JSONValue];
	
	if (jsonObject==nil) {
		
		NSLog(@"Failed to parse the JSON from the url supplied by AppleScript");
		jsonObject=@"";
		
	}
	
	// Use appscript framework to pack the object into something we can use
	
	asRecord =[codecs pack:jsonObject];
	
	
	[codecs release];
	[jsonString release];
	
	//Return the result to the applescript
	
	return asRecord;
	
	
	
}
	
@end
